package view.component.weapon
{
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.ButtonGroup;
	import feathers.controls.ScrollContainer;
	import feathers.data.ListCollection;
	
	import model.vo.weapon.BaseWeaponVo;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.events.Event;
	
	import util.FG2FUGenerator;
	
	import view.component.BaseListView;
	
	
	/**
	 * 武器简表基类
	 * @author £iang
	 * 
	 */	
	public class WeaponListView extends BaseListView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 锚点按钮组 */
		public var anchorTabBar:ButtonGroup;
		/** 锚点滚动容器 */
		public var anchorSC:ScrollContainer;
		
		/** 上一次武器路线 */
		public var lastRouteArr:Array = new Array();
		/** 锚点按钮组label名列表 */
		public var anchorlabelArr:Array;
		/** 锚点列表 */
		public var anchorArr:Array;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 列表中上次选中的选项下标 */
		protected var _lastSelectIndex:int = -1;
	
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{ 
			_itemH = UISize.GREAT_SWORD_ITEM_H * this.dpiScale;
			_listH = UISize.MAIN_H - 
				( UISize.MONSTER_TAB_AREA_H * 3 + 
					UISize.LIST_TITLE_H - 20 ) * this.dpiScale;
			_firstName = "名称";
			super.initialize();
			
			anchorTabBar = new ButtonGroup();
			anchorTabBar.direction = ButtonGroup.DIRECTION_HORIZONTAL;
			anchorTabBar.alpha = 0;
			anchorTabBar.gap = 15 * this.dpiScale;
				
			var dataProvider:ListCollection = new ListCollection( [] );
			var len:int = anchorArr.length;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				var obj:Object = new Object();
				obj.label = anchorlabelArr[ i ];
				obj.triggered = gotoAnchor;
				dataProvider.push( obj );
			}
			
			anchorTabBar.dataProvider = dataProvider;
			anchorTabBar.buttonFactory = mainTabFactory;
			
			anchorSC = new ScrollContainer();
			this.addChild( anchorSC );
			anchorSC.addChild( anchorTabBar );
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			anchorSC.x = 20 * this.dpiScale;
			anchorSC.y = _listH + ( UISize.LIST_TITLE_H + 20 ) * this.dpiScale;
			anchorSC.width = UISize.MAIN_W - 40 * this.dpiScale;
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			list.addEventListener( Event.CHANGE, listChangeHandler );
		}
		
		/**
		 * 处理列表选中某项后派发的事件 
		 * @param event 列表选中某项后派发的事件 
		 * 
		 */		
		protected function listChangeHandler( event:Event ):void
		{
			if ( list.selectedIndex >= 0 )
			{
				if ( _lastSelectIndex >= 0 )
					_listData.data[ _lastSelectIndex ].item.changeScrollerPage( 0 );
				
				_listData.data[ list.selectedIndex ].item.changeScrollerPage( 1 );
				_lastSelectIndex = list.selectedIndex;
			}
		}
		
		/**
		 * 点亮或熄灭武器升级路线 
		 * @param routeArr 升级路线
		 * 
		 */		
		public function setWeaponRoute( routeArr:Array, isSelected:Boolean ):void
		{
			var len:int = _listData.length;
			
			if ( isSelected )
				lastRouteArr = new Array();
			
			for ( var i:int = 0; i < len; i ++ )
			{
				var curWeaponId:int = ( _listData.data[ i ].itemData as BaseWeaponVo ).weaponId;
				var len2:int = routeArr.length;
				
				for ( var j:int = 0; j < len2; j ++ )
				{
					if ( curWeaponId == routeArr[ j ] )
					{
						_listData.data[ i ].item.isSelected = isSelected;
					
						if ( isSelected )
						{
							lastRouteArr.push( routeArr[ j ] );
						}
						break;
					}
				}
			}
		}
		
		/**
		 * 批量设置mainTabBar按钮 
		 * @return tabbar按钮
		 * 
		 */		
		private function mainTabFactory():Button
		{
			var button:Button = FG2FUGenerator.generateRoundRectButton(
				UISize.TAB_BUTTON_W, UISize.TAB_BUTTON_H,
				this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
			);
			
			return button;
		}
		
		override public function playShowAnimate():void
		{
			var tween:Tween = new Tween( anchorTabBar, 0.4, "linear" );
			
			tween.animate( "alpha", 1 );
			Starling.juggler.add( tween );
			super.playShowAnimate();
		}
		
		override public function playHideAnimate():void
		{
			var tween:Tween = new Tween( anchorTabBar, 0.4, "linear" );
			
			tween.animate( "alpha", 0 );
			Starling.juggler.add( tween );
			super.playHideAnimate();
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理点击跳转锚点按钮后派发的事件 
		 * @param event 点击跳转锚点按钮后派发的事件 
		 * 
		 */		
		protected function gotoAnchor( event:Event ):void
		{
			showItem( anchorArr[ anchorTabBar.getChildIndex( event.target as Button ) ] );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */
		override public function dispose():void
		{
			Starling.juggler.removeTweens( anchorTabBar );
			
			var button:Button;
			var k:int;
			
			for ( k = 0; k < anchorTabBar.numChildren; k ++ )
			{
				if ( anchorTabBar.getChildAt( k ) is Button )
				{
					button = anchorTabBar.getChildAt( k ) as Button;
					button.dispose();
					FG2FUGenerator.deepDisposeButtonComp( button );
					button = null;
				}
			}
			
			anchorSC.dispose();
			anchorSC = null;
			anchorArr.length = 0;
			anchorArr = null;
			anchorlabelArr.length = 0;
			anchorlabelArr = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function WeaponListView()
		{
			super();
		}
	}
}